using Billing2.Controls;
using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Security.Application;

namespace Billing2
{
    public class Subscriber : Page
    {
        public DataTable dt;
        protected ucPageTitle ucPageTitle1;
        protected UpdatePanel UpdatePanel5;
        protected ucSearch ucSearch;
        protected UpdatePanel UpdatePanel3;
        protected GridView gvView;
        protected UpdatePanel UpdatePanel4;
        protected UpdateProgress UpdateProgress1;
        protected UpdateProgress UpdateProgress2;
        protected Button btnPrint;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["SiteID"] == null)
                this.Response.Redirect("default.aspx",true);
            if (!this.Page.IsPostBack)
            {
                this.BindData();
                this.ViewState["SortDirection"] = "ASC";
            }
            this.GetPermission();
            //btnPrint.Visible = false;
        }

        private void GetDataSource()
        {
            if (this.ucSearch.FilterDT != null)
                this.dt = this.ucSearch.FilterDT;
            else if (this.dt == null)
                this.dt = this.getData();
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            this.BindData();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ViewStateUserKey = Session.SessionID;
        }

        private void BindData()
        {
            this.GetDataSource();
            if (this.dt != null)
            {
                this.gvView.DataSource = this.dt;
                this.gvView.DataBind();
            }
            if (this.gvView.Rows.Count <= 0 || this.Page.IsPostBack)
                return;
            this.ucSearch.SearchCriteriaSubscriber = ClsUtilityHelper.BindGridData(this.gvView);
        }

        protected void gvwCostCenter_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if ((e.CommandName).ToString().ToLower() != "editdata")
                return;
            new ClsCostCentresInSite().ID = Convert.ToInt64(e.CommandArgument);
            ClsUtilityHelper.OpenJQueryDialog("#dialogUpdate", (Control)this);
        }

        private void GetPermission()
        {
            ClsACLHelper clsAclHelper = new ClsACLHelper();
            string pageName = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
            clsAclHelper.SetPermission(LoginUser.TreeMenu, pageName);
            if (clsAclHelper.Read0)
                return;
            this.Response.Redirect("logout.aspx",true);
        }

        protected void gvView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataView dataView = new DataView(this.ucSearch.FilterDT);
            dataView.Sort = e.SortExpression + " " + this.ViewState["SortDirection"].ToString();
            this.ViewState["Sort"] = dataView.Sort;
            this.ucSearch.FilterDT = dataView.ToTable();
            if (this.ViewState["SortDirection"].ToString() == "ASC")
                this.ViewState["SortDirection"] = "DESC";
            else
                this.ViewState["SortDirection"] = "ASC";
        }

        public DataTable getData()
        {
            DataView dataView = new DataView(new ClsSubscriber().SelectBySiteID(LoginUser.SiteID));
            if (this.ViewState["Sort"] != null && this.ViewState["Sort"].ToString() != "")
                dataView.Sort = this.ViewState["Sort"].ToString();
            return dataView.ToTable();
        }

        protected void gvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvView.PageIndex = e.NewPageIndex;
            DataView dataView = new DataView(this.ucSearch.FilterDT);
            if (this.ViewState["Sort"] != null && this.ViewState["Sort"].ToString() != "")
                dataView.Sort = this.ViewState["Sort"].ToString();
            this.ucSearch.FilterDT = dataView.ToTable();
            this.gvView.DataSource = this.ucSearch.FilterDT;
            this.gvView.DataBind();
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            //this.Server.Transfer("SubscriberReportForm.aspx");
            //this.Response.Redirect("SubscriberReportForm.aspx");        
        }


    }
}
